Namespacing everything to /UVa.
[andmenj-acm.git] / UVa / 562 - Dividing coins / robado1.cpp
bloba4236b72722c8b71830f8373b35ddff28e0227cb
1 //Robado de: http://www.wretch.cc/blog/celiaailec/19569472
2 // Q562: Dividing coins
3 // 2008.05.29 Celia
4 // Method: Dynamic Programming
5 // Result: Accepted
6 // Time: 0.050
7 #include <stdio.h>
8 #include <string.h>
10 #define N 101
11 #define M 25001
13 bool v[M];
15 int iabs(int n)
17 return (n > 0)? n : -n;
20 int main()
22 int i, j, t, n, m, sum, a[N];
23 scanf("%d", &t);
25 while(t-- > 0)
27 scanf("%d", &n);
29 for(sum = 0, i = 1; i <= n; i++)
30 scanf("%d", &a[i]), sum += a[i];
32 m = sum / 2;
33 memset(v, 0, sizeof(v));
34 v[0] = true;
36 for(i = 1; i <= n; i++)
37 for(j = m; j >= 1; j--)
38 if(!v[j])
39 v[j] = (a[i] <= j)? v[j-a[i]] : false;
41 for(j = m; j >= 1; j--)
42 if(v[j])
43 break;
45 printf("%d\n", iabs(sum - j * 2));
48 return 0;